moba.buildings = {}
moba.buildings_projectiles = {}

function moba.buildings_allowaction(i)
	moba.buildings[tonumber(i)].canaction = true
end
function moba.building_reset(k)
	local building = moba.buildings[k]
	building.hp = moba.config.buildings[building.type].hp
	building.base = image(moba.config.buildings[building.type].base:format(building.team),0,1,3)
	building.image = image(moba.config.buildings[building.type].image:format(building.team), 0,1,3)
	imagepos(building.image, building.x, building.y, building.rot)
	imagepos(building.base, building.x, building.y, 0)
	building.canaction = true
end
function moba.buildings_load()
	for _,e in pairs(entitylist()) do
		local t = {
			"^mobatower(%d+)$",
			"^mobambarracks(%d+)-(%d+)$",
			"^mobarbarracks(%d+)-(%d+)$"
		}
		local name = string.lower(entity(e.x,e.y,"name") or "")
		local rot = entity(e.x,e.y,"int4") or 0
		local team,type,lane
		for k, n in pairs(t) do
			team,lane = string.match(name,n)
			if team then
				type = k
				break
			end
		end
		if type then
			local sz = moba.config.buildings[type].size
			local basic = {
				type = type,
				x = e.x*32+sz.w/2,
				y = e.y*32+sz.h/2,
				team = tonumber(team),
				lane = tonumber(lane),
				rot = rot
			}
			moba.buildings[#moba.buildings+1] = basic
			moba.building_reset(#moba.buildings)
		end
	end
end

addhook("startround","moba.buildings_reset")
function moba.buildings_reset()
	for k, building in pairs(moba.buildings) do
		moba.buildings[k] = nil
	end
	moba.buildings_load()
	for k, proj in pairs(moba.buildings_projectiles) do
		moba.turrets_destroyprojectile(k)
	end
end

function moba.turrets_findnewtarget(i)
	local turret = moba.buildings[i]
	local inradius = moba.pathfinding_inradius
	for k,id in pairs(player(0,"team"..(3-turret.team).."living")) do
		if inradius(turret.x,turret.y,player(id,"x"),player(id,"y"),moba.config.buildings[1].range) then
			return "players",id
		end
	end
	for k,creep in pairs(moba.creeps) do
		if creep.team == 3-turret.team then
			if inradius(turret.x,turret.y,creep.x,creep.y,moba.config.buildings[1].range) then
				return "creeps",k
			end
		end
	end
end

addhook("always","moba.buildings_always")
function moba.buildings_always()
	local inradius = moba.pathfinding_inradius
	for k,building in pairs(moba.buildings) do
		if building.type == 1 then
			local t,tt = building.target,building.target_t
			if not t or not inradius(building.x,building.y,moba.id_field(tt,t,"x"),moba.id_field(tt,t,"y"),moba.config.buildings[1].range) or moba.id_field(tt,t,"hp")<=0 then
				building.target_t, building.target = moba.turrets_findnewtarget(k)
			end
		end
		if building.canaction then
			if building.type==1 and building.target then
				local cfg = moba.config.buildings[building.type].projectiles
				moba.projectiles_spawn(
					building.x,building.y,
					"buildings", k, strings["team"..building.team].." HOMO",
					building.target_t,building.target,
					cfg.damage,cfg.speed,cfg.image:format(building.team)
				)
				building.canaction = false
				timer(moba.config.buildings[building.type].rate, "moba.buildings_allowaction", tostring(k),1)
			elseif building.type==2 or building.type==3 then
				local dx,dy = math.cos(math.rad(building.rot-90)),math.sin(math.rad(building.rot-90))
				local sz = moba.config.buildings[building.type].size
				moba.creeps_spawn(building.x+dx*sz.w/2,building.y+dy*sz.h/2,building.team,building.lane,building.type-1)
				local lp = moba.creeps_lanepaths[building.team][building.lane]
				moba.creeps_setpath(#moba.creeps,1,lp)
				moba.creeps_setdest(#moba.creeps,2,lp[1].x,lp[1].y)
				building.canaction = false
				timer(moba.config.buildings[building.type].rate, "moba.buildings_allowaction", tostring(k),1)
			end
		end
	end
end